package ija.view;

import ija.controller.GameController;
import ija.model.GameModel;
import ija.view.elements.Board;

import java.awt.Color;
import java.awt.Dimension;
import java.util.Observable;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.Border;

/**
 * Testing class for GUI operations
 */

public class BoardGameContent extends GameContent {

    private static final long serialVersionUID = -6219580699636610668L;
    protected JTextArea taMoveHistory;
    protected JScrollPane spMoveHistory;
    public JTextField tfMove;
    protected JLabel lMove, lMessage;
    public JButton bMove;
    protected Board pBoard;

    public Board getBoard() {
        return this.pBoard;
    }

    public void setBoard(Board pBoard) {
        this.pBoard = pBoard;
    }

    public BoardGameContent() {
        super();
    }

    public BoardGameContent(GameController gc) {
        super(gc);
    }

    public String GetMove() {
        String sMove = this.tfMove.getText();
        this.tfMove.setText("");
        return sMove;
    }

    @Override
    public void setEnabled(boolean b) {
        super.setEnabled(b);
        this.pBoard.setEnabled(b);
    }

    public void setPlayersEditable(boolean b) {
        this.pPlayer1.setEditable(b);
        this.pPlayer2.setEditable(b);
    }

    @Override
    protected void addWidgets(GameController gc) {
        super.addWidgets(gc);
        Border blackline = BorderFactory.createLineBorder(Color.lightGray);

        this.taMoveHistory = new JTextArea(5, 20);
        this.spMoveHistory = new JScrollPane(this.taMoveHistory);
        this.spMoveHistory.setBorder(BorderFactory.createTitledBorder(blackline, "Move history"));

        this.lMove = new JLabel("Move:", SwingConstants.LEFT);
        this.lMove.setMinimumSize(new Dimension(80, 20));
        this.lMove.setMaximumSize(new Dimension(80, 20));
        this.lMove.setPreferredSize(new Dimension(80, 20));

        this.tfMove = new JTextField(20);
        this.tfMove.setMinimumSize(new Dimension(80, 20));
        this.tfMove.setMaximumSize(new Dimension(80, 20));
        this.tfMove.setPreferredSize(new Dimension(80, 20));
        // this.tfMove.
        this.bMove = new JButton("Move");
        this.bMove.setPreferredSize(new Dimension(50, 20));
        this.bMove.setMinimumSize(new Dimension(50, 20));
        this.bMove.setMaximumSize(new Dimension(50, 20));
        this.bMove.addActionListener(this.getController().createMoveListener());

        this.pBoard = new Board(8, this);
        this.pBoard.setPreferredSize(new Dimension(470, 470));
        this.pBoard.setMaximumSize(new Dimension(510, 510));

        this.lMessage = new JLabel("Informační zpráva", SwingConstants.LEFT);
        this.lMessage.setMinimumSize(new Dimension(80, 20));
        this.lMessage.setMaximumSize(new Dimension(80, 20));
        this.lMessage.setPreferredSize(new Dimension(80, 20));

        GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        layout.setHorizontalGroup(layout
                .createSequentialGroup()
                .addGroup(
                        layout.createParallelGroup(GroupLayout.Alignment.LEADING, true)
                                .addComponent(this.pBoard)
                                .addGroup(
                                        layout.createSequentialGroup()
                                                .addComponent(this.lMove, 0,
                                                        GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(this.tfMove, 0,
                                                        GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(this.bMove, 0,
                                                        GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addComponent(this.spMoveHistory))
                .addGroup(
                        layout.createParallelGroup(GroupLayout.Alignment.LEADING, true)
                                .addComponent(this.pPlayer1, GroupLayout.PREFERRED_SIZE,
                                        GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE)
                                .addComponent(this.pPlayer2, GroupLayout.PREFERRED_SIZE,
                                        GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE)
                                .addComponent(this.lMessage, GroupLayout.PREFERRED_SIZE,
                                        GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE)
                                .addComponent(this.bStartGame, 0, GroupLayout.DEFAULT_SIZE,
                                        Short.MAX_VALUE)
                                .addComponent(this.bExitGame, 0, GroupLayout.DEFAULT_SIZE,
                                        Short.MAX_VALUE)
                                .addComponent(this.bSaveGame, 0, GroupLayout.DEFAULT_SIZE,
                                        Short.MAX_VALUE)
                                .addComponent(this.bLoadGame, 0, GroupLayout.DEFAULT_SIZE,
                                        Short.MAX_VALUE)
                                .addComponent(this.bMainMenu, 0, GroupLayout.DEFAULT_SIZE,
                                        Short.MAX_VALUE)));

        layout.setVerticalGroup(layout
                .createSequentialGroup()
                .addGroup(
                        layout.createParallelGroup(GroupLayout.Alignment.LEADING, true)
                                .addComponent(this.pBoard, 0, GroupLayout.DEFAULT_SIZE,
                                        Short.MAX_VALUE)
                                .addGroup(
                                        layout.createSequentialGroup().addComponent(this.pPlayer1)
                                                .addComponent(this.pPlayer2)
                                                .addComponent(this.lMessage)))
                .addGroup(
                        layout.createParallelGroup(GroupLayout.Alignment.LEADING, true)
                                .addGroup(
                                        layout.createSequentialGroup()
                                                .addGroup(
                                                        layout.createParallelGroup(
                                                                GroupLayout.Alignment.LEADING, true)
                                                                .addComponent(this.lMove)
                                                                .addComponent(this.tfMove)
                                                                .addComponent(this.bMove))
                                                .addComponent(this.spMoveHistory))
                                .addGroup(
                                        layout.createSequentialGroup()
                                                .addComponent(this.bStartGame, 0,
                                                        GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(this.bExitGame, 0,
                                                        GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(this.bSaveGame, 0,
                                                        GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(this.bLoadGame, 0,
                                                        GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(this.bMainMenu, 0,
                                                        GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))));
    }

    @Override
    public void update(Observable o, Object obj) {
        if (o instanceof GameModel) {
            // ((GameModel) o).getDesk()
        }
    }
}
